#include "student.h"
#include "ui_student.h"
#include<QDebug>
#include<QSqlQuery>
#include<QVariant>
#include<QVariantList>
#include<QSqlRecord>
#include<QAbstractItemView>
#include<QModelIndex>
#include<QMessageBox>
//Student::Student(QWidget *parent) :
//    QWidget(parent),
//    ui(new Ui::Student)

Student::Student(QString id,QString name):
    ui(new Ui::Student)
{
    this->m_id=id;
    this->m_name=name;
//    qDebug()<<this->m_id<<this->m_name;
    ui->setupUi(this);
//    setFixedSize(700,600);
    setMinimumSize(700,600);

    setWindowTitle("欢迎登录预约系统");

    //设置控件字体大小
    QFont ft;
    ft.setPointSize(12);
    ui->label->setFont(ft);
    ui->label_2->setFont(ft);
    ui->label_3->setFont(ft);
    ui->pushButton->setFont(ft);
    ui->pushButton_2->setFont(ft);
    ui->pushButton_3->setFont(ft);
    ui->pushButton_4->setFont(ft);
    ft.setPointSize(10);
    ui->comboBox->setFont(ft);
    ui->comboBox_2->setFont(ft);
    ui->comboBox_3->setFont(ft);
    ui->comboBox->setFixedSize(QSize(170, 25));
    ui->comboBox_2->setFixedSize(QSize(170, 25));
    ui->comboBox_3->setFixedSize(QSize(170, 25));
    mode=new QSqlTableModel(this);
    mode->setTable("order_info");
    ui->tableView->setModel(mode);
    mode->select();
    mode->setHeaderData(0,Qt::Horizontal,"预约序号");
    mode->setHeaderData(1,Qt::Horizontal,"用户ID");
    mode->setHeaderData(2,Qt::Horizontal,"姓名");
    mode->setHeaderData(3,Qt::Horizontal,"日期");
    mode->setHeaderData(4,Qt::Horizontal,"时间段");
    mode->setHeaderData(5,Qt::Horizontal,"机房号");
    mode->setHeaderData(6,Qt::Horizontal,"机房容量");
    mode->setHeaderData(7,Qt::Horizontal,"预约状态");

}

Student::~Student()
{
    delete ui;
}
//申请预约
void Student::on_pushButton_clicked()
{
    QString date=ui->comboBox->currentText();
    QString time=ui->comboBox_2->currentText();
    QString roomName=ui->comboBox_3->currentText();
    int roomId=0;
    int roomSize=0;
    if(roomName=="1号机房")
    {
        roomId=1;
        roomSize=30;
    }
    else if(roomName=="2号机房")
    {
        roomId=2;
        roomSize=50;
    }
    else
    {
        roomId=3;
        roomSize=80;
    }
    QSqlQuery query;
    QString str=QString("insert into order_info(user_id,user_name,DATE,TIME,room_id,room_size,STATUS) values('%1','%2','%3','%4',%5,%6,'审核中');")
            .arg(this->m_id).arg(this->m_name).arg(date).arg(time).arg(roomId).arg(roomSize);
    query.exec(str);


}

  //查看所有预约信息
void Student::on_pushButton_3_clicked()
{
    ui->tableView->setModel(mode);
//    mode->select();
//    mode->setHeaderData(0,Qt::Horizontal,"预约序号");
//    mode->setHeaderData(1,Qt::Horizontal,"用户ID");
//    mode->setHeaderData(2,Qt::Horizontal,"姓名");
//    mode->setHeaderData(3,Qt::Horizontal,"日期");
//    mode->setHeaderData(4,Qt::Horizontal,"时间段");
//    mode->setHeaderData(5,Qt::Horizontal,"机房号");
//    mode->setHeaderData(6,Qt::Horizontal,"机房容量");
//    mode->setHeaderData(7,Qt::Horizontal,"预约状态");
    mode->select();
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    mode->setFilter("");
}
  //查看我的预约
void Student::on_pushButton_2_clicked()
{
    ui->tableView->setModel(mode);
    mode->select();
//    mode->setHeaderData(0,Qt::Horizontal,"预约序号");
//    mode->setHeaderData(1,Qt::Horizontal,"用户ID");
//    mode->setHeaderData(2,Qt::Horizontal,"姓名");
//    mode->setHeaderData(3,Qt::Horizontal,"日期");
//    mode->setHeaderData(4,Qt::Horizontal,"时间段");
//    mode->setHeaderData(5,Qt::Horizontal,"机房号");
//    mode->setHeaderData(6,Qt::Horizontal,"机房容量");
//    mode->setHeaderData(7,Qt::Horizontal,"预约状态");
    mode->setFilter(QString("user_id='%1'").arg(this->m_id));
}
  //取消预约
void Student::on_pushButton_4_clicked()
{
    mode->setEditStrategy(QSqlTableModel::OnManualSubmit);
//    mode->setHeaderData(0,Qt::Horizontal,"预约序号");
//    mode->setHeaderData(1,Qt::Horizontal,"用户ID");
//    mode->setHeaderData(2,Qt::Horizontal,"姓名");
//    mode->setHeaderData(3,Qt::Horizontal,"日期");
//    mode->setHeaderData(4,Qt::Horizontal,"时间段");
//    mode->setHeaderData(5,Qt::Horizontal,"机房号");
//    mode->setHeaderData(6,Qt::Horizontal,"机房容量");
//    mode->setHeaderData(7,Qt::Horizontal,"预约状态");

    //获取选中的模型
    QItemSelectionModel *sMode=ui->tableView->selectionModel();
    //取出模型中的索引
    QModelIndexList list=sMode->selectedRows();
    if(list.size()!=0) //未选中行时，让取消按钮不起作用
    {
        QAbstractItemModel *model=ui->tableView->model();
        for(int i=0;i<list.size();i++)
        {
            //获取选中行的用户ID
            QModelIndex index = model->index(list.at(i).row(),1);
            QVariant data = model->data(index);
            QString data1=data.toString(); //uid
            qDebug()<<this->m_id<<data1;
            if(this->m_id==data1)
            {
                mode->removeRow(list.at(i).row());
            }
    //        mode->removeRow(list.at(i).row());
        }
        int flag=QMessageBox::warning(this,"取消预约","确定取消预约吗？",QMessageBox::Yes,QMessageBox::No);
        if(flag==QMessageBox::No)
        {
            //撤销删除动作
            mode->revertAll();
            mode->submitAll();
        }
        else
        {
            mode->submitAll();
        }
    }
}
